<?php

global $AppUI, $df, $m, $tab, $task_not_quotation;

require_once($AppUI->getModuleClass('companies'));
require_once($AppUI->getModuleClass('suppliers'));
require_once($AppUI->getModuleClass('items'));
require_once($AppUI->getModuleClass('tasks'));
require_once($AppUI->getModuleClass('projects'));
require_once($AppUI->getMasterDataClass('mdtype'));
require_once($AppUI->getMasterDataClass('mdstatus'));
require_once $AppUI->getModuleClass('admin');
require_once $AppUI->getModuleClass('contacts');

$df = $AppUI->getPref('SHDATEFORMAT');

if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('TASK_POST');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('TASK_POST');
	}
	else {
		$_POST = $AppUI->getPOST('TASK_POST');
	}
}

//$task_not_quotation = intval( dPgetParam( $_GET, 'task_not_quotation', 0 ) );

$page_offset = intval( dPgetParam( $_GET, 'page_offset', 0 ) );
//$page_limit = intval( dPgetParam( $_POST, 'page_limit', $AppUI->getPref('PAGELIMIT', 5) ) );
$page_limit = intval( dPgetParam( $_POST, 'page_limit', 999999));
$status_id = intval( dPgetParam( $_POST, 'status_id', 0 ) );
$item_type = intval( dPgetParam( $_POST, 'item_type', 0 ) );
$customer = intval( dPgetParam( $_POST, 'customer', 0 ) );
$project_shipping_type = intval( dPgetParam( $_POST, 'project_shipping_type', 0 ) );
$keyword = dPgetParam( $_POST, 'keyword', '' );
/*
$project_name = dPgetParam( $_POST, 'project_name', '' );
$project_code = dPgetParam( $_POST, 'project_code', '' );
*/
$from_date = dPgetParam( $_POST, 'from_date', '' );
$to_date = dPgetParam( $_POST, 'to_date', '' );
//do search
$q = new DBQuery();
$q->dropTemp('pitems, wunit, vunit, qunit, amounts');
$q->exec();
$q->clear();

$q->createTemp('wunit');
$q->addTable('md_unit');
$q->addQuery('unit_id as wunit_id, unit_name as wunit_name');
$q->exec();
$q->clear();

$q  = new DBQuery;
$q->createTemp('qunit');
$q->addTable('md_unit');
$q->addQuery('unit_id as qunit_id, unit_name as qunit_name');
$q->exec();
$q->clear();

// Volumn Unit
$q  = new DBQuery;
$q->createTemp('vunit');
$q->addTable('md_unit');
$q->addQuery('unit_id as vunit_id, unit_name as vunit_name');
$q->exec();
$q->clear();

$q->createTemp('pitems');
$q->addTable('items');
$q->leftJoin('wunit', 'wu', 'item_weight_unit = wu.wunit_id');
$q->leftJoin('qunit', 'qu', 'item_quantity_unit = qu.qunit_id');
$q->leftJoin('vunit', 'vu', 'item_volume_unit = vu.vunit_id');
if ($item_type > 0) {
	$q->addWhere("item_type1 = $item_type");
}
$q->exec();
$q->clear();

$q->createTemp('amounts');
$q->addTable('task_log');
$q->addQuery('task_log_task, SUM(task_log_amount) as sum_amount');
$q->addGroup('task_log_task');
$q->exec();
$q->clear();

$projectCompletedSysVal = dPgetSysVal( 'ProjectCompleted' );

//main query for tasks
$q->addTable('tasks');
$q->innerJoin('projects', 'pro', 'project_id = task_project');
$q->innerJoin('pitems', 'pi', 'item_id = task_item');
$q->leftJoin('quotations', 'q', 'quote_id = task_quotation ');
$q->leftJoin('amounts', 'a', 'task_log_task = task_id ');

if ($task_not_quotation > 0) {
	$q->addWhere('(task_quotation IS NULL or task_quotation = 0)');
}
else{
	$user = new CUser();
	$user->load($AppUI->user_id);
	$contact = new CContact();
	$contact->load($user->user_contact);

	$q->addWhere('task_division='. $contact->contact_division);
	$q->addWhere('task_costcenter='. $contact->contact_costcenter);
}
//$q->addWhere('task_project = ' . $project_id);
$q->addOrder('task_start_date desc, task_name');
//$q->setLimit($page_limit, $page_offset);
//echo $q->prepare();
$qTotal = $q->prepare();
$q->addQuery('distinct tasks.*, pro.*, q.*, pi.*, a.*');
$tasks = $q->loadList();

// Load Object Status
$objMD = new CMdStatus();
$statList = $objMD->getStatusList('Reference');

$objMD = new CMdType();
$itemtypeList = $objMD->getTypeList('ItemType1');

$objMD = new CCompany();
$cusList = $objMD->getAllowedRecords($AppUI->user_id, 'company_id, company_name', 'company_name');
$cusList = arrayMerge(array('0'=>"" . $AppUI->_(''). ""), $cusList);

?>
<table border="0" cellpadding="2" cellspacing="0" width="100%" class="infopanel">
<form name="frmFilter" action="index.php" method="get">
<tr>
	<td width="98%">&nbsp;</td>
	<td width="1%">
		<input id="idHIA" type="checkbox" name="task_not_quotation" <?=$task_not_quotation > 0?'checked':''?>
			value="1" onchange="document.frmFilter.submit()">
	</td>
	<td nowrap="nowrap"><label for="idHC"><?=$AppUI->_('Task not link quotation')?></label></td>
	<td><img src="images/blank.gif" width="8"></td>
</tr>
<input type="hidden" name="m" value="projects"/>
<input type="hidden" name="a" value="view"/>
<!--input type="hidden" name="project_id" value="<?=$project_id?>"/-->
<input type="hidden" name="tab" value="<?=$tab?>"/>
</form>
</table>
<table id="idTasksList" class="list" cellspacing="0" cellpadding="2" border="0" width="100%">
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
			<th >&nbsp;</th>
			<th >&nbsp;</th>
			<th align="right"><?=$AppUI->_('%')?></th>
			<th ><?=$AppUI->_('Code')?></th>
			<th ><?=$AppUI->_('Name')?></th>
			<th ><?=$AppUI->_('Quotation')?></th>
			<th ><?=$AppUI->_('Amounts')?></th>
			<th ><?=$AppUI->_('Figure')?></th>
			<th ><?=$AppUI->_('Quantity')?></th>
			<th ><?=$AppUI->_('Weight')?></th>
			<th ><?=$AppUI->_('Volume')?></th>
			<th ><?=$AppUI->_('Start date')?></th>
			<th ><?=$AppUI->_('End date')?></th>
			<th ><?=$AppUI->_('Remarks')?></th>
		</tr>
<?php
if (is_array($tasks) && count($tasks) > 0) {
	$totalRows = 0;
	$qTotal = str_ireplace('select *', 'select count(*) as total_rows', $qTotal);
	$tmp = db_loadList($qTotal);
	$totalRows = $tmp[0]['total_rows'];

	require_once($AppUI->getLibraryClass('codeigniter/pagination'));
	$pagination = new CI_Pagination();
	$config = array(
		'base_url' => '?m=projects&tab=1',
		'total_rows' => $totalRows,
		'per_page' => $page_limit

	);
	$pagination->initialize($config);
	$i = 0;
	?>

	<script type="text/javascript">
		function submitForm(offset) {
			document.reportFrm.offset.value = offset;
			document.reportFrm.submit();
		}
	</script>
	<?
	$sumAmounts = 0.00;
	$lastProject;
	$i = 1;
	foreach ($tasks as $row) {
		$sumAmounts += $row['sum_amount'];
		$i++;
		$start_date = new CDate($row['task_start_date']);
		$end_date = new CDate($row['task_end_date']);
		if ($lastProject != $row['task_project']) {
			$referenceURL = "?m=projects&a=view&project_id=" . $row['task_project'];
	?>
				<tr class="first">
					<td colspan="100%">
						<a href="<?=$referenceURL?>" style="border: 1px outset rgb(238, 238, 238); background-color: rgb(255, 255, 255); padding: 3px;"><b><?=$row['project_name']?></b></a>
					</td>
				</tr>
	<?php
			$lastProject = $row['task_project'];
		}
		?>
		<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>" onClick="showTask(<?=$row['task_id']?>)">
			<td align='center'>
				<a href="?m=timecard&a=vw_newlog&project_id=<?=$row['task_project']?>&task_id=<?=$row['task_id']?>"><image src="images/add_log.gif" border="0" alt="Add log"/></a>
			</td>
			<td align='center'>
				<a href="?m=tasks&a=simpleaddedit&task_id=<?=$row['task_id']?>"><image src="images/edit.gif" border="0" alt="Edit"/></a>
			</td>
			<td align='right'><?=$row['task_percent_complete']?></td>
			<td ><?=$row['item_code']?></td>
			<td ><?=$row['task_name']?></td>
			<td ><?=$row['quote_name']?></td>
			<td align='right'><?=number_format($row['sum_amount'])?></td>
			<td align='right'><?=number_format($row['task_figure'])?></td>
			<td align='right'><?=number_format($row['task_quantity_unit'])?></td>
			<td align='right'><?=number_format($row['task_weight_unit'])?></td>
			<td align='right'><?=number_format($row['task_volume_unit'])?></td>
			<td ><?=$start_date->format($df)?></td>
			<td ><?=$end_date->format($df)?></td>
			<td ><?=$row['task_description']?></td>
		</tr>
		<?
	}
	?>
	<tr>
		<td colspan="4" align="right">Total: </td>
		<td align="right"><?=number_format($sumAmounts)?></td>
		<td colspan="100%"></td>
	</tr>
	</table>

	<?
} else {
	showEmptyRow('1', 'second', 'Not found any tasks', '100%');
}

?>
</table>
<script>
	Calendar.enabled = true;

	Table("idTasksList").setNormalHover(1);

	function showTask(id)
	{
		location.href = "index.php?m=tasks&a=view&task_id=" + id;
	}

	function doSearch(frm) {
		frm.action = '?m=projects&tab=1&suppressHeaders=0';
		frm.submit();
	}

	function doExport(frm) {
		//frm.action = '?m=reports&a=do_r_reference&suppressHeaders=1';
		//frm.submit();
	}
</script>